//
//  UserAccountViewModel.swift
//  JMWB
//
//  Created by 个人电脑 on 16/3/20.
//  Copyright © 2016年 个人电脑. All rights reserved.
//

import UIKit

typealias SucceessCallBack = (isSuccess: Bool) ->()

class UserAccountViewModel{

   static let shareIntance: UserAccountViewModel = UserAccountViewModel()
    var account: UserAccount?
    var isLogin: Bool{
        return account != nil && !isExpire
    }
    var isExpire: Bool{
        guard let expires_date = account?.expires_date else{
        return true
        }
        return expires_date.compare(NSDate()) == NSComparisonResult.OrderedAscending
    }
    //沙盒路径的计算属性
    var accountPath: String{
        let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first!
        return (path as NSString).stringByAppendingPathComponent("account.plist")
    }
    
    
    init(){
    // 1.获取沙盒路径
//       var path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first!
//        path = (path as NSString).stringByAppendingPathComponent("account.plist")
    // 2.读取对象
        account = NSKeyedUnarchiver.unarchiveObjectWithFile(accountPath) as? UserAccount
        
    }
    
}
extension UserAccountViewModel{
//    func loadAccessToken(codeString :String, isSuccess :(isSuccess: Bool) ->()){
    func loadAccessToken(codeString :String, isSuccess :SucceessCallBack){
        
        //        NetworkTools.shareIntance.loadAccessToken(codeString)
        NetworkTools.shareIntance.loadAccessToken(codeString) { (result, error) -> () in
            // 错误校验
            if error != nil{
                print(error)
                isSuccess(isSuccess: false)
                return
            }
            //获取字典
            guard let accountDict = result else{
                isSuccess(isSuccess: false)
                return
            }
            //字典转成模型对象
            let account = UserAccount(dict: accountDict)
            //请求用户信息
            self.loadUserInfo(account, isSuccess: isSuccess)
        }
    }
    
    
//    func loadUserInfo(account: UserAccount,isSuccess :(isSuccess: Bool) ->()){
    func loadUserInfo(account: UserAccount,isSuccess :SucceessCallBack){
        // 1.获取AccessToken和uid
        guard let accessToken = account.access_token,uid = account.uid else{
            isSuccess(isSuccess: false)
            return
        }
        // 2.请求用户信息
        NetworkTools.shareIntance.loadUserInfo(accessToken, uid: uid) { (result, error) -> () in
            // 错误校验
            if error != nil{
                print(error)
                isSuccess(isSuccess: false)
                return
            }
            //获取字典
            guard let userInfoDict = result else{
                isSuccess(isSuccess: false)
                return
            }
            //从字典中获取头像的地址和昵称
            account.avatar_large = userInfoDict["avatar_large"] as? String
            account.screen_name = userInfoDict["screen_name"] as? String
//            print(account)
            // 将账号的对象进行归档
            // 1.获取沙盒路径
//            var path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first!
//            path = (path as NSString).stringByAppendingPathComponent("account.plist")
            //保存账号信息
            
            NSKeyedArchiver.archiveRootObject(account, toFile: self.accountPath)
           // 将创建出来的用户账号对象,赋值给account属性
            self.account = account
            //回调成功(加载信息成功)
            isSuccess(isSuccess: true)
        }
    }
}

